package com.huizhonglm.diap.widget.expandTab;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-17 21:33
 */
public class MenuGridAdapter extends BaseAdapter {

    private Context mContext;
    private MenuInfo[] mArrayData;
    private String selectedText = "";
    private int selectedPos = -1;
    private float textSize;
    private View.OnClickListener onClickListener;
    private OnItemClickListener mOnItemClickListener;

    public MenuGridAdapter(Context context, MenuInfo[] arrayData) {
        mContext = context;
        mArrayData = arrayData;
        init();
    }

    private void init() {
        onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Integer selPos = (Integer) view.getTag();
                if (selPos.compareTo(selectedPos) == 0) {
                    selectedText = "";
                    selectedPos = -1;
                    notifyDataSetChanged();
                } else {
                    selectedPos = selPos;
                    setSelectedPosition(selectedPos);
                }
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, selectedPos);
                }
            }
        };
    }

    /**
     * 设置列表字体大小
     */
    public void setTextSize(float tSize) {
        textSize = tSize;
    }

    /**
     * 设置选中的position,并通知列表刷新
     */
    public void setSelectedPosition(int pos) {
        if (mArrayData != null && pos < mArrayData.length) {
            selectedPos = pos;
            selectedText = mArrayData[pos].getName();
            notifyDataSetChanged();
        }
    }

    /**
     * 设置选中的position,但不通知刷新
     */
    public void setSelectedPositionNoNotify(int pos) {
        selectedPos = pos;
        if (mArrayData != null && pos < mArrayData.length) {
            selectedText = mArrayData[pos].getName();
        }
    }

    @Override
    public int getCount() {
        return EmptyUtil.isEmpty(mArrayData) ? 0 : mArrayData.length;
    }

    @Override
    public Object getItem(int position) {
        return EmptyUtil.isEmpty(mArrayData) ? null : mArrayData[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView view;
        if (convertView == null) {
            view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.widget_extab_list_item, parent, false);
        } else {
            view = (TextView) convertView;
        }
        view.setTag(position);
        String mString = "";
        if (mArrayData != null) {
            if (position < mArrayData.length) {
                mString = mArrayData[position].getName();
            }
        }

        if (mString.contains("不限"))
            view.setText("不限");
        else
            view.setText(mString);

        view.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

        if (selectedText != null && selectedText.equals(mString)) {
            view.setBackgroundResource(R.drawable.btn_login);//设置选中的背景图片
            view.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        } else {
            view.setBackgroundResource(R.drawable.bg_transparent);//设置未选中状态背景图片
            view.setTextColor(ContextCompat.getColor(mContext, R.color.yahei));
        }
        view.setPadding(0, 0, 0, 0);
        view.setGravity(Gravity.CENTER);
        view.setOnClickListener(onClickListener);
        return view;
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    /**
     * 重新定义菜单选项单击接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
